<?php

namespace App\Http\Controllers\WxApi\Handles;

use App\Http\Controllers\WxApi\Handle;
use App\Http\Controllers\WxApi\Traits\Reply;
use App\Models\WeChat;
use Houdunren\WeChat\Message;
use Illuminate\Support\Facades\Log;

class Text extends Handle
{
    use Reply;
    public function handle()
    {
        if ($this->message->isText()) {
            //查找默认消息是否是关键词，如果不是则原样返回
            //遍历数组中的方法，如果有返回值，则返回。
            foreach (['keyword', 'defaultMessage'] as $action) {
                if ($content = $this->$action()) {
                    return $content;
                }
            }
        }
    }

    protected function keyword()
    {

        return $this->reply($this->message->Content);

    }

    protected function defaultMessage()
    {

        if ($this->model->default_message) {
            //默认回复内容先进行关键字对比。如果不符合关键字要求，再直接回复
            if ($content = $this->reply($this->model->default_message)) {
                return $content;
            }
            return $this->message->text($this->model->default_message);
        }

    }
}
